package com.smart_campus.sys.service.impl;

import com.smart_campus.sys.common.Constant;
import com.smart_campus.sys.common.DataGridView;
import com.smart_campus.sys.mapper.MenuMapper;
import com.smart_campus.sys.mapper.RoleMapper;
import com.smart_campus.sys.vo.MenuVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.smart_campus.sys.domain.Menu;
import com.smart_campus.sys.service.MenuService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author Alfred
 * @date 2020/6/8 19:51
 */
@Service
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuMapper menuMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return menuMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(Menu record) {
        return menuMapper.insert(record);
    }

    @Override
    public int insertSelective(Menu record) {
        return menuMapper.insertSelective(record);
    }

    @Override
    public Menu selectByPrimaryKey(Integer id) {
        return menuMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(Menu record) {
        return menuMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Menu record) {
        return menuMapper.updateByPrimaryKey(record);
    }

    @Override
    public List<Menu> queryAllMenuForList(Map<String,Object> map) {

        List<Menu> menus = menuMapper.queryAllMenuForList(map);
        return menus;
    }
    @Override
    public List<Menu> queryMenuForListUserById(Map<String,Object> map,Integer id) {
        //根据userid查询角色id的集合
        List<Integer> roleIds = this.menuMapper.queryRoleIdsByUserId(id);
        //根据角色id的集合，查询菜单的id集合
        if(roleIds!=null&&roleIds.size()>0){
            List<Integer> menuIds = this.menuMapper.queryMenuIdsByRids(roleIds);
            if(menuIds!=null&&menuIds.size()>0){
                List<Menu> menus = this.menuMapper.queryMenuForListUserById(map,menuIds);
                return menus;
            }else{
                return new ArrayList<>();
            }
        }else {
            return new ArrayList<>();
        }
    }

    /**
     * 查询菜单和权限
     * @return
     */
    @Override
    public DataGridView queryAllMenu(Menu menu) {
        List<Menu> menus = this.menuMapper.queryAllMenu(menu);
        return new DataGridView(Long.valueOf(menus.size()),menus);
    }

    /**
     * 查询最大的排序码
     * @return
     */
    @Override
    public Integer queryMenuMaxOrderNum() {
        return this.menuMapper.queryMenuMaxOrderNum();
    }

    @Override
    public Menu getById(Integer id) {
        return menuMapper.getMenuById(id);
    }

    /**
     * 根据id查询当前菜单和权限的子菜单和权限的个数
     * @param id
     * @return
     */
    @Override
    public Integer queryMenuChildrenCountById(Integer id) {
        return this.menuMapper.queryMenuChildrenCountById(id);
    }

    /**
     * 根据用户id查询权限编码集合
     * @param userId
     * @return
     */
    @Override
    public List<String> queryPermissionCodesByUserId(Menu menu,Integer userId) {
        //根据userid查询角色id的集合
        List<Integer> roleIds=this.roleMapper.queryRoleIdsByUserId(userId);
        //根据角色id的集合，查询菜单id的集合
        if(roleIds!=null&&roleIds.size()>0){
            List<Integer> menuIds=this.roleMapper.queryMenuIdsByRids(roleIds);
            if(menuIds!=null&&menuIds.size()>0){
                List<Menu> menus = this.menuMapper.selectLists(menu,menuIds);
                List<String> permissions=new ArrayList<>();
                for (Menu menu1 : menus) {
                    permissions.add(menu1.getTypecode());
                }
                return permissions;
            }else{
                return null;
            }
        }else{
            return null;
        }
    }

}

